﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;

namespace XinYi.CrookedShow.DAL
{
    public class DbContextFactory<BS> where BS : DbContext, new()
    {
        /// <summary>
        /// 唯一实例
        /// </summary>
        /// <returns></returns>
        public static DbContext GetGurrentDbContext()
        {
            //CallContext保证实例唯一
            var dbContext = CallContext.GetData(typeof(BS).Name) as DbContext;
            //不为空直接返回
            if (dbContext != null) return dbContext;
            //为空则new 添加类条件new()使BS能够实例化
            dbContext = new BS();
            //重新设置
            CallContext.SetData(typeof(BS).Name, dbContext);
            return dbContext;
        }
    }
}
